<?
//Category 0.0.6
class sym_category extends symbiont{
    public function ___main($design, $for='', $id=0){
        global $kernel, $sql;
        $this->for_symbiont=data::word($for);
        $design=$this->_check($design, 'main');
        $d=new design($design, 'category', false);
        if(!data::is_int($id)) $id=$sql->select_each('sym_category', 'id', array('name'=>data::word($id)), '', '', 1);
        else $id=data::number($id);
        $this->pages='';
        $categories=$sql->query_each("SELECT c.id, c.cover, c.name, p.title, p.content as description FROM `sym_category` as c LEFT JOIN `paragraphs` as p ON p.id=c.paragraph_id AND p.lng_id = ".$kernel->lang->id." WHERE c.parent_id=".$id." ".($for?"AND c.for='".$for."'":'')." ORDER BY `position`");
        $this->content='';
        $this->count=1;
        if($categories){
            $i=0;
            foreach($categories as $val){
                $i++;
                $this->id=$val['id'];
                $this->name=$val['name'];
                $this->title=$val['title']?$val['title']:$val['name'];
                $this->cover=$val['cover'];
                $this->description=$val['description'];
                $this->content.=design::run($this->category);
            }
        }
        $d->inc();
    }
    public function ___titles($design, $for='', $levels=1, $id=0){
        global $kernel, $sql;
        $this->for_symbiont=data::word($for);
        $design=$this->_check($design, 'titles');
        $d=new design($design, 'category', false);
        if(!data::is_int($id)) $id=$sql->select_each('sym_category', 'id', array('name'=>data::word($id)), '', '', 1);
        else $id=data::number($id);
        $this->content=$this->title($for, $id, $levels);
        $d->inc();
    }
    private function title($for='', $id=0, $levels=1){
        global $sql, $kernel;
        $this->pages='';
        $f=($for?"AND c.for='".$for."'":'');
        $categories=$sql->query_each("
                                    SELECT c.id, c.name, p.title, exists(
                                        SELECT 1
                                        FROM `sym_category`
                                        WHERE c.parent_id=".$id." ".$f."
                                    ) as sub
                                    FROM `sym_category` as c
                                        LEFT JOIN `paragraphs` as p
                                           ON p.id=c.paragraph_id AND p.lng_id = ".$kernel->lang->id."
                                    WHERE c.parent_id=".$id." ".$f."
                                    ORDER BY `position`
                                    ");
        $this->count=1;
        $content='';
        if($categories){
            $i=0;
            foreach($categories as $val){
                $i++;
                if($val['sub']) $this->sub=$this->title($for, $val['id'], $levels?$levels-1:0);
                else $this->sub='';
                $this->id=$val['id'];
                $this->name=$val['name'];
                $this->title=$val['title']?$val['title']:$val['name'];
                $content.=design::run($this->category);
            }
        }
        return $content;
    }
    public function ___admin($design, $for='', $id=0){
        global $kernel, $sym;
        $kernel->symbiont_add('category-admin');
        $sym->category_admin->___main($design, $for, $id);
    }
}
?>